#ifndef MODULE_GENERAL_REG_H
#define MODULE_GENERAL_REG_H
#include <stdint.h>
#include <module_header_reg.h>
#define DATA_STORAGE_MOTOR_NUM 3
#define DATA_STORAGE_MODULE_GENERAL_SIZE sizeof(module_general_reg_t)
//-------------- 1个字节 -------------------
//寄存器0x40：保留
typedef struct{
    union{
        struct{
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t m3     : 1;
            uint8_t        : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}master_update_data_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x41：Motor(1-3)中断寄存器
typedef struct{
    union{
        struct{
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t m3     : 1;
            uint8_t        : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}slave_interrupt_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x42：Motor(1-3)设置更新
typedef struct{
    union{
        struct{
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t m3     : 1;
            uint8_t        : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}slave_update_setup_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x43：Motor(1-3) Motor使能
typedef struct{
    union{
        struct{
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t m3     : 1;
            uint8_t        : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}control_enable_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x44：Motor(1-3)运动模式
typedef struct{
    union{
        struct{
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t m3     : 1;
            uint8_t        : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}stepper_move_mode_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x45：保留

//-------------- 4x3=12个字节 -------------------
//寄存器 0x46-0x49,0x4A-0x4D,0x4E-0x51：Motor(1-3)总的累计运动步数
typedef struct{
    union{
            // struct{
            //     uint8_t low_8bit;
            //     uint8_t high_8bit;
            // }steps_u16;
            // struct{
            //     uint8_t low_8bit;
            //     uint8_t high_8bit;
            // }delay_u16;
        uint32_t value;
        uint8_t data[4];
    };
}stepper_total_motion_steps_stats_reg_t;

//-------------- 2x3=6个字节 -------------------
//寄存器 0x52-0x53,0x54-0x5,0x56-0x57：Motor(1-3)加减速度
typedef struct{
    union{
        struct{
            uint8_t low_8bit;
            uint8_t high_8bit;
        };
        uint16_t value;
        uint8_t data[2];
    };
}stepper_acc_and_dec_stats_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x58：Motor(1-3)当前状态
typedef struct{
    union{
        union{
            struct{
                uint8_t value    : 2;
                uint8_t          : 6;
            }m1;

            struct{
                uint8_t          : 2;
                uint8_t value    : 2;
                uint8_t          : 4;
            }m2;

            struct{
                uint8_t          : 4;
                uint8_t value    : 2;
                uint8_t          : 2;
            }m3;
        };
        uint8_t value;
        uint8_t data[1];
    };
}stepper_current_state_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x59，0x5A, 0x5B：Motor(1-3)联动步数
typedef struct{
    union{
        uint8_t value;
        uint8_t data[1];
    };
}stepper_linkage_steps_stats_reg_t;

//-------------- 2个字节 -------------------
//寄存器 0x5C-0x5D, 0x5E-0x5F, 0x60-0x61：Motor(1-3)联动循环次数
typedef struct{
    union{
        struct{
            uint8_t low_8bit;
            uint8_t high_8bit;
        };
        uint16_t value;
        uint8_t data[2];
    };
}stepper_total_cycle_count_setup_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x62：Motor(1-3)运动触发
typedef struct{
    union{
        struct{
            uint8_t m1t     : 1;
            uint8_t m2t     : 1;
            uint8_t m3t     : 1;
            uint8_t         : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}stepper_move_trigger_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x63, 0x64 ,0x65：Motor(1-3)步进电机细分
typedef struct{
    union{
        struct{
            uint8_t m0     : 1;
            uint8_t m1     : 1;
            uint8_t m2     : 1;
            uint8_t         : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}stepper_step_resolution_setup_reg_t;

//-------------- 1个字节 -------------------
//寄存器 0x66,0x67, 0x68：Motor(1-3)步进电机限流
typedef struct{
    union{
        struct{
            uint8_t s0    : 1;
            uint8_t s1    : 1;
            uint8_t s2    : 1;
            uint8_t       : 5;
        };
        uint8_t value;
        uint8_t data[1];
    };
}stepper_current_limit_setup_reg_t;



typedef struct{
    union{
        long value;//4字节
        char data[4];
    };
}current_motion_steps_stats_reg_t;

//-------------- 90个字节 -------------------
//寄存器 0x69-0xF9：MUX(1-3)联动分配寄存器

typedef struct{
    union{
        struct{
            struct{
                struct{
                    uint8_t low_8bit;
                    uint8_t high_8bit;
                };
                uint16_t u16_value;
                uint8_t data[2];
            }steps;
            struct{
                struct{
                    uint8_t low_8bit;
                    uint8_t high_8bit;
                };
                uint16_t u16_100ms;
                uint8_t data[2];
            }delay;
        };
        uint32_t value;
        uint8_t data[4];
    };
}smc_node_reg_t;

// typedef struct{
//     union{
//         uint8_t data[30*4];
//         smc_node_reg_t smc_node_reg[30];
//     };
// }stepper_linkage_allocation_reg_t;
//-------------- 15个字节 -------------------
// 模块信息查询结构体定义
typedef struct
{
    union{
        struct
        {   
            current_motion_steps_stats_reg_t                           current_motion_steps_stats_reg;//4字节
            stepper_total_motion_steps_stats_reg_t                       total_motion_steps_stats_reg;//4字节:0x46-0x51
            stepper_acc_and_dec_stats_reg_t                                     acc_and_dec_setup_reg;//2字节:0x52-0x57
            stepper_linkage_steps_stats_reg_t                           total_linkage_steps_stats_reg;//1字节:0x59-0x5B
            stepper_total_cycle_count_setup_reg_t                         total_cycle_count_setup_reg;//2字节:0x5C-0x61
            stepper_step_resolution_setup_reg_t                             step_resolution_setup_reg;//1字节:0x63-0x65
            stepper_current_limit_setup_reg_t                               current_limit_setup_reg;//1字节:0x66-0x68           
        };
        
        uint8_t data[15];
    };
} module_general_info_t;
//-------------- 186个字节 -------------------
typedef struct
{
    union{
        struct
        {   
            master_update_data_reg_t                        master_update_data_reg;//1字节:0x40
            slave_interrupt_reg_t                              slave_interrupt_reg;//1字节:0x41
            slave_update_setup_reg_t                        slave_update_setup_reg;//1字节:0x42
            control_enable_reg_t                                control_enable_reg;//1字节:0x43s

            stepper_move_mode_reg_t                                motion_mode_reg;//1字节:0x44
            module_reserved_reg_t                           module_reserved_45_reg;//1字节:0x45
            stepper_current_state_reg_t                        current_state_reg;//1字节:0x58
            stepper_move_trigger_reg_t                          motion_trigger_reg;//1字节:0x62
            
            module_general_info_t                        module_general_info[DATA_STORAGE_MOTOR_NUM];//15*3=45字节 + 8字节

            //stepper_linkage_allocation_reg_t          stepper_linkage_allocation_reg;//120字节:120+45 + 8
                    
        };
        uint8_t data[53];
    };
}module_general_reg_t;


#endif